package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarModelMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarModel;
import com.wanghaixin.pojo.CarModelExample;
import com.wanghaixin.service.CarModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 19:03
 * @Description: 车系管理 服务层
 */
@Service
public class CarModelServiceImpl implements CarModelService {

    @Autowired
    private CarModelMapper carModelMapper;

    @Override
    public int addCarModel(CarModel carModel) {
        int i = carModelMapper.insertSelective(carModel);
        return i;
    }

    @Override
    public void delCarModel(Integer id) {
        int i = carModelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int upCarModel(CarModel carModel) {
        int update = carModelMapper.updateByPrimaryKeySelective(carModel);
        return update;
    }

    @Override
    public CarModel findById(Integer id) {
        CarModel carModel = carModelMapper.selectByPrimaryKey(id);
        return carModel;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        CarModelExample example = new CarModelExample();
        if (!StringUtils.isEmpty(queryDTO.getSort())) {
            example.setOrderByClause("id");
        }
        List<CarModel> carModels = carModelMapper.selectByExample(example);
        PageInfo<CarModel> carModelPageInfo = new PageInfo<>(carModels);
        long total = carModelPageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, carModels);
        return dataGridResult;
    }
}
